//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class LoadingUI extends eui.Group implements RES.PromiseTaskReporter {
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.createView, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.destroy, this);
        egret.MainContext.instance.stage.addEventListener(egret.Event.RESIZE, this.modifyBody, this)
    }
    private guanggao: eui.Image;
    private pgBg: eui.Image;
    private pgBar: eui.Image;
    private textField: eui.Label;
    private w: number = 0;
    private h: number = 0;
    private _shpBeMask: egret.Shape;
    private group: eui.Group
    private DragonBonesLIST: Array<dragonBones.EgretArmatureDisplay> = [];
    private createView(): void {
        this.w = egret.MainContext.instance.stage.stageWidth;
        this.h = egret.MainContext.instance.stage.stageHeight;
        this.width = egret.MainContext.instance.stage.stageWidth;
        this.height = egret.MainContext.instance.stage.stageHeight


        var bgImg = new eui.Image("login_bg")
        bgImg.left = 0
        bgImg.right = 0
        bgImg.top = 0
        bgImg.bottom = 0
        this.addChild(bgImg)

        var group = new eui.Group();
        var factory2 = dragonBones.EgretFactory.factory;
        factory2.parseDragonBonesData(RES.getRes("gengxin_meinv_ske_json"));
        factory2.parseTextureAtlasData(RES.getRes("gengxin_meinv_tex_json"), RES.getRes("gengxin_meinv_tex"));
        var armatureDisplay2 = factory2.buildArmatureDisplay("gengxin_meinv");
        armatureDisplay2.animation.play();
        group.addChild(armatureDisplay2)
        group.left = 200
        group.bottom = -105
        this.addChild(group);
        this.DragonBonesLIST.push(armatureDisplay2)

        var group2 = new eui.Group();
        var factory3 = dragonBones.EgretFactory.factory;
        factory3.parseDragonBonesData(RES.getRes("logo_ske_json"));
        factory3.parseTextureAtlasData(RES.getRes("logo_tex_json"), RES.getRes("logo_tex"));
        var armatureDisplay3 = factory3.buildArmatureDisplay("logo");
        armatureDisplay3.animation.play();
        group2.addChild(armatureDisplay3)
        group2.horizontalCenter = 0
        group2.top = 300
        this.addChild(group2);
        this.DragonBonesLIST.push(armatureDisplay3)

        //进度条
        var group3 = new eui.Group()
        this.pgBg = new eui.Image("PreLoadingBarBg");
        group3.addChild(this.pgBg);

        this.pgBar = new eui.Image("PreLoadingBar");
        this.pgBar.x = 1;
        this.pgBar.y = 1;
        group3.addChild(this.pgBar);

        this.textField = new eui.Label();
        this.textField.horizontalCenter = 0
        this.textField.y = 50;
        this.textField.text = "0%";
        this.textField.fontFamily = "Microsoft YaHei"
        this.textField.bold = true
        this.textField.size = 20
        this.textField.textColor = 0xFFF9DC
        group3.addChild(this.textField);

        this.guanggao = new eui.Image("hall_gg");
        this.guanggao.horizontalCenter = 0;
        this.guanggao.y = -50
        group3.addChild(this.guanggao);

        group3.horizontalCenter = 0;
        group3.bottom = 60
        this.addChild(group3);
        this.group = group3;
    }
    private destroy() {
        egret.MainContext.instance.stage.removeEventListener(egret.Event.RESIZE, this.modifyBody, this)
        //龙骨动画销毁
        for (let i = 0; i < this.DragonBonesLIST.length; i++) {
            if (this.DragonBonesLIST[i] && this.DragonBonesLIST[i].parent) {
                this.DragonBonesLIST[i].animation.stop()
                this.DragonBonesLIST[i].dispose();
                this.DragonBonesLIST[i] = null;
            }
        }
    }
    public modifyBody() {
        this.w = egret.MainContext.instance.stage.stageWidth;
        this.h = egret.MainContext.instance.stage.stageHeight;
        this.width = egret.MainContext.instance.stage.stageWidth;
        this.height = egret.MainContext.instance.stage.stageHeight
    }
    public onProgress(current: number, total: number): void {
        var rate: number = Math.round((current / total) * 100);
        this.textField.text = rate + "%";
        var w = this.pgBar.width * (rate / 100);

        if (this.group.contains(this._shpBeMask)) {
            this.group.removeChild(this._shpBeMask)
            this._shpBeMask = null;
            this.pgBar.mask = null;
        }
        this._shpBeMask = new egret.Shape;
        this._shpBeMask.graphics.beginFill(0xffffff);
        this._shpBeMask.graphics.drawRect(0, 0, w, this.pgBar.height);
        this._shpBeMask.graphics.endFill();
        this.group.addChild(this._shpBeMask);
        this._shpBeMask.x = this.pgBar.x;
        this._shpBeMask.y = this.pgBar.y;
        this.pgBar.mask = this._shpBeMask;
    }
}
